//
//  WeChatMomentsTableView.m
//  WeChatMoments
//
//  Created by admin on 2019/8/27.
//  Copyright © 2019 SilverFruity. All rights reserved.
//

#import "HeaderRefreshTableView.h"

@interface HeaderRefreshTableView()
@property (nonatomic, strong)UIImageView *headeRefreshIndicator;
@property (nonatomic, strong)UIView  *loadMoreIndicator;
@property (nonatomic, assign)UIGestureRecognizerState panGestureState;
@end

@implementation HeaderRefreshTableView
- (instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style{
    self = [super initWithFrame:frame style:style];
    [self initRefreshHeader];
    [self addObservers];
    return self;
}
- (instancetype)initWithCoder:(NSCoder *)aDecoder{
    self = [super initWithCoder:aDecoder];
    [self initRefreshHeader];
    [self addObservers];
    return self;
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context{
    if ([keyPath isEqualToString:@"state"]) {
        UIGestureRecognizerState new = (UIGestureRecognizerState)[change[NSKeyValueChangeNewKey] integerValue];
        UIGestureRecognizerState old = (UIGestureRecognizerState)[change[NSKeyValueChangeOldKey] integerValue];
        if (old == UIGestureRecognizerStateChanged && new == UIGestureRecognizerStateEnded && [self headerRefreshVisiable]) {
            [self headerRefreshStartAnimation];
            if (self.headerRefreshActionBlock) {
                self.headerRefreshActionBlock();
            }
        }
    }
}
- (void)initRefreshHeader{
    self.headeRefreshIndicator = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"loading"]];
    [self hideHeaderRefresh];
}
- (void)addObservers{
    [self.panGestureRecognizer addObserver:self forKeyPath:@"state" options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld context: nil];
}
- (void)setHeaderRefreshSuperView:(UIView *)view{
    [view addSubview:self.headeRefreshIndicator];
}
- (void)dealloc{
    [self.panGestureRecognizer removeObserver:self forKeyPath:@"state"];
}

- (BOOL)headerRefreshVisiable{
    return !self.headeRefreshIndicator.hidden;
}
- (void)showHeaderRefresh{
    [self bringSubviewToFront:self.headeRefreshIndicator];
    self.headeRefreshIndicator.hidden = NO;
    [UIView animateWithDuration:0.25 animations:^{
        self.headeRefreshIndicator.frame = CGRectMake(20, 84, 30, 30);
        self.headeRefreshIndicator.alpha = 1;
    }];
}
- (void)hideHeaderRefresh{
    [UIView animateWithDuration:0.25 animations:^{
        self.headeRefreshIndicator.frame = CGRectMake(20, 0, 30, 30);
        self.headeRefreshIndicator.alpha = 0;
    } completion:^(BOOL finished) {
        self.headeRefreshIndicator.transform = CGAffineTransformIdentity;
        self.headeRefreshIndicator.hidden = YES;
        [self.headeRefreshIndicator.layer removeAllAnimations];
    }];
}
- (void)headerRefreshStartAnimation{
    self.headeRefreshIndicator.transform = CGAffineTransformIdentity;
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
    animation.toValue = @(2 * M_PI);
    animation.repeatCount = MAXFLOAT;
    animation.duration = 1;
    animation.removedOnCompletion = NO;
    [self.headeRefreshIndicator.layer addAnimation:animation forKey:@"rotationAnimation"];
}
- (BOOL)headerRefreshIsAnimating{
    return [self.headeRefreshIndicator.layer animationForKey:@"rotationAnimation"] != nil;
}

- (void)headerRefreshEndRefreshing{
    [self hideHeaderRefresh];
}
- (void)headerRefreshBeginRefresh{
    [self showHeaderRefresh];
    [self headerRefreshStartAnimation];
    if (self.headerRefreshActionBlock) {
        self.headerRefreshActionBlock();
    }
}
- (void)setContentOffset:(CGPoint)contentOffset{
    [super setContentOffset:contentOffset];
    CGPoint offset = contentOffset;
    CGFloat boundaryValue = -64;
    
    if (offset.y < boundaryValue && ![self headerRefreshVisiable]) {
        [self showHeaderRefresh];
    }else if (offset.y < boundaryValue && [self headerRefreshVisiable] && ![self headerRefreshIsAnimating]) {
        CGFloat oneCircleValue = 64;
        CGFloat value = (offset.y - boundaryValue)/oneCircleValue * M_PI * 2;
        self.headeRefreshIndicator.transform = CGAffineTransformRotate(CGAffineTransformIdentity, value);
    }
    else if (offset.y > boundaryValue && [self headerRefreshVisiable] && ![self headerRefreshIsAnimating]){
        [self hideHeaderRefresh];
    }
//    if (offset.y >= (self.contentSize.height - self.frame.size.height) && ![self loadMoreIndicatorVisiable]) {
//        [self showLoadMoreIndicator];
//        [self loadMoreIndicatorStartAnimation];
//    }else if ([self loadMoreIndicatorVisiable]){
//        [self hideLoadMoreIndicator];
//    }
}
@end
